using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Bordecal.ImportsSorter.Configuration
{
	/// <summary>
	/// Dialog form for adding a new namespace prefix to the sorting groups list.
	/// </summary>
	internal sealed partial class AddSortingGroupForm : Form
	{
		/// <summary>
		/// Initializes a new instance of the AddSortingGroupForm class.
		/// </summary>
		private AddSortingGroupForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Sets the dialog result when the OK button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The data for the event.</param>
		private void btnOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
		}

		/// <summary>
		/// Sets the dialog result when the Cancel button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The data for the event.</param>
		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		/// <summary>
		/// Displays an instance of the AddSortingGroupForm dialog for entry of a new sorting group namespace prefix.
		/// </summary>
		/// <param name="autoCompleteEntries">An array of strings to be used as auto-complete entries for the namespace prefix text box.</param>
		/// <param name="modalParent">The form that should be used as the parent for the modal dialog.</param>
		/// <returns>The sorting group prefix specified by the user.  (A null value will be returned if the user cancels the dialog.)</returns>
		internal static string AddSortingGroup(string[] autoCompleteEntries, Form modalParent)
		{
			string retVal = null;

			using (AddSortingGroupForm additionForm = new AddSortingGroupForm())
			{
				if ((autoCompleteEntries != null) && (autoCompleteEntries.Length > 0))
				{
					AutoCompleteStringCollection autoCompleteSourceList = new AutoCompleteStringCollection();
					autoCompleteSourceList.AddRange(autoCompleteEntries);

					additionForm.txtNewGroupPrefix.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
					additionForm.txtNewGroupPrefix.AutoCompleteSource = AutoCompleteSource.CustomSource;
					additionForm.txtNewGroupPrefix.AutoCompleteCustomSource = autoCompleteSourceList;
				}

				if (additionForm.ShowDialog(modalParent) == DialogResult.OK)
					retVal = additionForm.txtNewGroupPrefix.Text.Trim();
			}

			return retVal;
		}
	}
}